#include "tcp.h"

Epoll*  Tcp::epoll = nullptr;
Pool*   Tcp::pool  = nullptr;


Tcp::Tcp()
{
    cout << "tcp" << endl;

    epoll = Epoll::GetInstance();
    pool = Pool:: GetInstance();
}

Tcp::~Tcp()
{
    cout << "~tcp" << endl;
    if(epoll != nullptr)
        delete epoll;
    if(pool != nullptr)
        delete epoll;
}


//　开始
void Tcp::Begin(task_t fun)
{

    this->Set_ListensEvent_Callback(fun);

    pthread_create(&this->tcp_id,NULL,
                    [](void*arg)->void*
                    {
                        epoll->ListensEvents();
                        return NULL;
                    },
                    NULL
                    );
}
void Tcp::End(void)
{


    if(epoll != nullptr)
        delete epoll;
    if(pool != nullptr)
        delete epoll;
}

unsigned int Tcp::GetListensEventsCount(void)
{
    return epoll->Get_Cur_Listeners();
}
unsigned int Tcp::GetTaskCount(void)
{
    return  pool->Get_TaskCount();
}
unsigned int Tcp::GetThreadCount(void)
{
    return pool->Get_Active();
}

void Tcp::Set_ThreadCount(unsigned int num)
{
    pool->Add_Thread(num);
}

bool Tcp::Set_ListensEvent_Callback(task_t fun)
{
    bool flag = false;
    if(fun == nullptr)
    {
        flag = false;
    }
    else
    {
        pool->Set_TaskFun(fun);
        flag = true;
    }
    return flag;
    
}







